package com.xq.jltx.actiontoolsbar;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;

import com.xq.jltx.utils.XqToast;

import java.lang.reflect.Method;

public class CustomBarActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_bar);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        String title = bundle.getString("title");
        if (!TextUtils.isEmpty(title)) {
            setTitle(title);
        }
        initActionBar();
    }

    private void initActionBar() {
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowHomeEnabled(false);   //是否在Title旁边是否显示程序logo图标，默认是true显示logo图标，设置false表示不显示
        actionBar.setHomeButtonEnabled(true);  //设置已显示的logo图标是否可点击,默认为false不可点击
/*        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_TITLE);
        actionBar.setCustomView(R.layout.actionbar_back_btn);
        actionBar.getCustomView().setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                XqToast.showToastShortMsg(CustomBarActivity.this,"返回上一个页面");
                finish();
            }
        });*/
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.custom_action_menu, menu);
        return true;
    }

    /**
     * 设置menu显示icon
     */
    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {

        if (featureId == Window.FEATURE_ACTION_BAR && menu != null) {
            if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
                try {
                    Method m = menu.getClass().getDeclaredMethod(
                            "setOptionalIconsVisible", Boolean.TYPE);
                    m.setAccessible(true);
                    m.invoke(menu, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return super.onMenuOpened(featureId, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                XqToast.showToastShortMsg(this, "返回上一个页面");
                finish();
                break;
            case R.id.share:
                XqToast.showToastShortMsg(this, "share");
                break;
            case R.id.undo:
                XqToast.showToastShortMsg(this, "undo");
                break;
            case R.id.redo:
                XqToast.showToastShortMsg(this, "redo");
                break;
            case R.id.save:
                XqToast.showToastShortMsg(this, "save");
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
